package com.boot2551.control;

import com.boot2551.bean.Emp;
import com.boot2551.service.DeptS;
import com.boot2551.service.EmpS;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Controller
 * @ResponseBody2个注解等效于@RestController
 */
@RestController
@RequestMapping(value = "emp")
public class EmpC {
    @Resource
    public EmpS es;

    /**
     * pic与页面的图片名对应，员工的其它信息注入到Emp中
     * @param e
     * @param pic
     * @return
     */
    @RequestMapping(value = "/insert")
    public int insert(Emp e, MultipartFile pic) throws IOException {
        if (pic!=null){
            String fname=pic.getOriginalFilename();
            if (fname!=null&&!fname.equals("")){
                e.setEpic(fname);
                e.setEpic2(pic.getInputStream());
            //上传
            String path2=System.getProperty("user.dir")+"/pic/";
            String filename=System.currentTimeMillis()+"_"+fname;
            Path filePath= Paths.get(path2+filename);

            File dir=new File(path2);
            if (!dir.exists()){
                dir.mkdir();
            }
            pic.transferTo(filePath.toFile());
                System.out.println("文件上传成功");
            }
            return es.insert(e);
        }
        return 0;
    }
    @RequestMapping(value = "/all")
    public List<Emp> all(HttpServletRequest req) throws IOException {
        //服务器路径
        String path=req.getServletContext().getRealPath("upload");
        File f=new File(path);
        //获取项目路径
        String path2=System.getProperty("user.dir")+"/pic/";
        System.out.println(path2);
        if (!f.exists()){
            f.mkdir();
        }
        return es.all(path2);
    }
    @RequestMapping(value = "/del")
    public int del(int id){
        return  es.delete(id);
    }
    @Resource
    public DeptS ds;
    @RequestMapping(value = "/one")
    public Map<String,Object> one(HttpServletRequest req, int id) throws IOException {
        //服务器路径
        String path=req.getServletContext().getRealPath("upload");
        File f=new File(path);
        if (!f.exists()){
            f.mkdir();
        }
        Map<String,Object> data=new HashMap<>();
        data.put("one",es.one(id,path));
        data.put("list",ds.all());
        return data;
    }
    @RequestMapping(value = "/update")
    public int update(Emp e, MultipartFile pic) throws IOException {
        if (pic!=null){
            String fname=pic.getOriginalFilename();
            if (fname!=null&&!fname.equals("")){
                e.setEpic(fname);
                e.setEpic2(pic.getInputStream());
            }
        }
            return es.updateselective(e);
    }
}
